# -*- coding: utf-8 -*-
# @Time : 2022/5/31 10:52
# @Author : WangYunfei
# @FileName: base_spider.py
# @Email: yunfei.wang@innvote.com


import random

import requests


class FakeChromeUserAgent(object):
    """ 伪造 谷歌浏览器请求头 user-agent """

    @classmethod
    def user_agent(cls):
        first_num = random.randint(55, 102)
        third_num = random.randint(0, 4999)
        fourth_num = random.randint(0, 140)
        # 系统版本
        os_type = ["(Windows NT 10.0; Win64; x64)", "(X11; Linux x86_64)"]
        # 谷歌浏览器
        chrome_version = f"Chrome/{first_num}.0.{third_num}.{fourth_num}"
        return " ".join(['Mozilla/5.0', random.choice(os_type), 'AppleWebKit/537.36',
                         '(KHTML, like Gecko)', chrome_version, 'Safari/537.36'])


class Spiders(FakeChromeUserAgent):

    def fetch(self, url, param=None, headers={}):
        headers["user-agent"] = self.user_agent()

        response = requests.get(url, params=param, headers=headers)
        if requests.status_codes == 200:
            return response
        return None


if __name__ == '__main__':
    print(FakeChromeUserAgent.user_agent())
    print(FakeChromeUserAgent.user_agent())
    print(FakeChromeUserAgent.user_agent())

